

import os

import requests
from lxml import etree

if __name__ == "__main__":
    url = 'https://sc.chinaz.com/tupian/rentiyishu.html'
    headers ={
        'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers)
    response.encoding = 'utf-8'
    page_text = response.text
    #print(page_text)
    tree = etree.HTML(page_text)
    a_list = tree.xpath('//div[@class="item"]')#/html/body/div[3]/div[2]/div[1]
    #print(a_list)
    if not os.path.exists('./picture'):
       os.mkdir('./picture')
    for a in a_list:
        a_herf = 'https:'+a.xpath('./img/@src')[0] + '.jpg'#https://scpic.chinaz.net/files/default/imgs/2022-09-20/86113e92801ac661_s.jpg
                                                            #//scpic.chinaz.net/files/default/imgs/2022-09-20/86113e92801ac661_s.jpg
        a_name = a.xpath('./img/@alt')[0]

        down_w = requests.get(url=a_herf,headers=headers).text

        b_tree = etree.HTML(down_w)
        b_list = b_tree.xpath('//div[@class="tupian-right-infor-top"]/p | ./a/@href')[0]
        b_data = requests.get(url=a_herf,headers=headers).content
        b_path = 'picture/'+a_name
        with open(b_path,'wb') as fp:
            fp.write(b_data)
            print('完成！')


